<template>
    <div class="search">
        <Header title="房屋管理" >
            <img class="left" @click="back" src="../assets/turn.png" alt="">
        </Header>
        <!-- 整体 -->
        <div class="die_wrapper">
            <!-- 上半部分 -->
            <div class="die_header">
                <div class="die_top">
                    <img src="../assets/icon_search.png">
                    <input type="text" v-model="key" placeholder="请输入搜索词例如0101室">
                </div>
            </div>
            <!-- 上半部分 -->
            <!-- 下半部分 -->
            <div class="die_footer" v-for="(item,index) in list" :key="index" @click="state(item.unitId)">
                <div class="die_bottom">
                    <p>{{item.name}}</p>
                    <p class="right" v-if="item.count !== 'N' && item.count !== 0">{{item.count}}<span>人</span></p>
                    <p class="right" v-else-if="item.count === 0">空置</p>
                    <p class="right" v-else></p>
                </div>
            </div>
            <!-- 下半部分 -->
        </div>
        <!-- 整体 -->
    </div>
</template>

<script>
import Header from './header'
import { statistic } from "../old_request"
export default {
    data(){
        return {
            list:[],
            key:''
        }
    },
    components:{
        Header
    },
    methods:{   
        back(){
            this.$router.back();
        },
        state(unitid){
            this.$router.push({
                name:'state',
                params:{unitid:unitid}
            })
        },
        statisc(){
            let that = this;
        
            let data = {
                sign: that.$md5(`${that.wrapper.globalData()}blockId=${that.$store.state.blockid}timestamp=${that.wrapper.gettime()}`),
                timestamp: that.wrapper.gettime(),
                blockId: that.$store.state.blockid,
                userId: that.$store.state.userid
            };

            statistic(data,that).then( (res) => {
                //console.log(res)
                if(res.code === 0){
                    that.list = res.data.unit;
                    for(let i=0;i<that.list.length;i++){            //冒泡排序 从高到底
                        for(let n=0;n<that.list.length-1;n++){
                            if(that.list[n].count < that.list[n+1].count){
                                [that.list[n],that.list[n+1]] = [that.list[n+1],that.list[n]];
                            }
                        }
                    }
                }
            })
        }
    },
    created(){
        let that = this;
        if(that.$store.state.blockid === null){
            that.wrapper.displace(that.$store.state.blockid,'login','item',that);
        }
        that.statisc();
    },
    watch:{
        key(value){                             //监听输入框的内容 实现搜索功能
            let that = this;
            let person = [],old,flag;
            if(value === ''){               //输入框为空则重新请求一次
                that.statisc();
            }
            for(let i=0;i<that.list.length;i++){
                if(that.list[i].name.indexOf(value) !== -1){    //判断输入的内容是否匹配的上
                    person.push(that.list[i]);                  //匹配的上就写入person
                    person.length > 0 ? flag = true : flag = false;
                }
                else if(that.list[i].name.indexOf(value) === -1){      //匹配不上时执行此语句
                    old = {name:'找不到此房屋!',count:'N'};
                }
            }
            if(!flag){                  //找不到住户就执行此语句
                person.push(old);
            }
            that.list = person;
        }
    }
}
</script>

<style scoped>
    .left{
        position: absolute;
        left: -6px;
        transform: translateY(1px) !important;
        height: 22px !important;
    }
    .search{
        margin-top: 50px;
    }
    .die_header{
        display: flex;
        background: #EFEFF4;
        height: 50px;
        line-height: 50px;
    }
    .die_header .die_top{
        width: 90%;
        margin: 0 auto;
        height: 30px;
        line-height: 30px;
        margin-top: 10px;
        border-radius:5px;
        background: white;
    }
    .die_top img{
        background: white;
        padding-left: 55px;
        transform: translateY(2px);
    }
    .die_top input{
        border: none;
        outline: none;
        width: 60%;
    }
    .die_footer{
        height: 45px;
        line-height: 45px;
        padding: 0px 3% 0px 6%;
        border:1px solid #EFEFF4;
        color: #666666;
    }
    .die_footer .die_bottom{
        display: flex;
    }
    .die_bottom .right{
        position: absolute;
        right: 6%;
    }
</style>